//#define __FORCE_TRACE__
//#define __FORCE_DEBUG__
//#define __FORCE_INFO__
#define THIS_FILE "MVTTraining\main.sqf"
#include <functions\core_macros.h>

TRACE("Start");
TMFPROFILERSTART;

private ["_rectangle","_marker_name","_inc","_safe_zone"];

QGVAR(pveh) addPublicVariableEventHandler FUNC(runEvent);
GVAR(ObjectStore) = "Land_HelipadEmpty_F" createVehicleLocal [0,0,0];
GVAR(ObjectStore) allowDamage false;


// Make walls invulnerable
if (markerType GVAR(MARKER_NAME_OBJECTS) != "") then {
	GVAR(MVT_OBJ_POS) = markerPos GVAR(MARKER_NAME_OBJECTS);
	GVAR(MVT_OBJ_POS) set [2,0];
	_size = markerSize GVAR(MARKER_NAME_OBJECTS);
	_rectangle = (markerShape GVAR(MARKER_NAME_OBJECTS) == "RECTANGLE");
	GVAR(MVT_OBJ_SIZE) = (_size select 0) max (_size select 1);
	GVAR(MARKER_NAME_OBJECTS) setMarkerAlpha 0;
	
	GVAR(MVT_OBJ_SHAPE) = [GVAR(MARKER_NAME_OBJECTS)] call tmf_fnc_toShape;
	
	_walls = nearestObjects [GVAR(MVT_OBJ_POS), ["Land_Shoot_House_Wall_F","Land_Pallet_F"], GVAR(MVT_OBJ_SIZE)];
	TRACE_1("Walls",count _walls);
	{
		_x addEventHandler ["HandleDamage", {0}];
	} foreach _walls;
} else {
	ERROR("MVT_Training_Ground_Objects marker not found!"),
};

if (markerType GVAR(MARKER_NAME) != "") then {
	GVAR(MVT_POS) = markerPos GVAR(MARKER_NAME);
	GVAR(MVT_POS) set [2,0];
	_size = markerSize GVAR(MARKER_NAME);
	_rectangle = (markerShape GVAR(MARKER_NAME) == "RECTANGLE");
	GVAR(MVT_SIZE) = (_size select 0) max (_size select 1);
	GVAR(MARKER_NAME) setMarkerAlpha 0;

	GVAR(MVT_SHAPE) = [GVAR(MARKER_NAME)] call tmf_fnc_toShape;
} else {
	ERROR("MVT_Training_Ground marker not found!"),
};

if (markerType "MVT_STREET" != "") then { "MVT_STREET" setMarkeralpha 0; };
if (markerType "MVT_SQUARE" != "") then { "MVT_SQUARE" setMarkeralpha 0; };
if (markerType "MVT_SQUARE_IN" != "") then { "MVT_SQUARE_IN" setMarkeralpha 0; };
if (markerType "MVT_SQUARE_OUT" != "") then { "MVT_SQUARE_OUT" setMarkeralpha 0; };

TRACE_1("Adding Events",QUOTE(FUNC(registerEvent)));
[0,"AddScore",FUNC(addScore)] call FUNC(registerEvent);
[1,"ResetScores",FUNC(resetScores)] call FUNC(registerEvent);
[1,"ResetTargets",FUNC(resetTargets)] call FUNC(registerEvent);
[0,"SkipTime",FUNC(skipTime)] call FUNC(registerEvent);
[0,"DisableTargets",{
	GVAR(AUTO_POP_TARGETS) = !GVAR(AUTO_POP_TARGETS);
	if (isServer && !GVAR(AUTO_POP_TARGETS)) then { [] call FUNC(resetTargets); };
	if (!isDedicated) then { if (GVAR(AUTO_POP_TARGETS)) then { hint localize "STR_MVT_ENABLED_TARGETS";} else { hint localize "STR_MVT_DISABLED_TARGETS";} };
}] call FUNC(registerEvent);

if ((GVAR(MVT_POS) distance [0,0,0]) > 0 && count GVAR(MVT_OBJ_POS) > 0) then {
	// Detect Building markers
	_inc = 1;
	_marker_name = format["MVT_Building_%1",_inc];
	while {markerType _marker_name != ""} do {
		_marker_name setMarkerAlpha 0;
		_inc = _inc + 1;
		_marker_name = format["MVT_Building_%1",_inc];
	};
	GVAR(BUILDINGS) = _inc - 1;
	TRACE_1("Buildings",GVAR(BUILDINGS));

	// Detect Crossing markers
	_inc = 1;
	_marker_name = format["MVT_crossing_%1",_inc];
	while {markerType _marker_name != ""} do {
		_marker_name setMarkerAlpha 0;
		_inc = _inc + 1;
		_marker_name = format["MVT_crossing_%1",_inc];
	};
	GVAR(CROSSINGS) = _inc - 1;
	TRACE_1("Crossings",GVAR(CROSSINGS));
	
	// Detect Square Exits markers
	_inc = 1;
	_marker_name = format["MVT_Square_Exit_%1",_inc];
	while {markerType _marker_name != ""} do {
		_marker_name setMarkerAlpha 0;
		_inc = _inc + 1;
		_marker_name = format["MVT_Square_Exit_%1",_inc];
	};
	GVAR(SQUARE_EXITS) = _inc - 1;
	TRACE_1("Square Exists",GVAR(SQUARE_EXITS));

	GVAR(TARGETS) = [];
	if (isServer) then {
		GVAR(TARGETS) = nearestObjects [GVAR(MVT_OBJ_POS), ["TargetP_Inf_F"], GVAR(MVT_OBJ_SIZE)];
		TRACE_1("Targets",count GVAR(TARGETS));
		[] spawn FUNC(registerTargets);
		
		[] spawn FUNC(targetRaiser);
	};
	
	if (!isDedicated) then {
		_control_center = missionNamespace getVariable ["MVT_ControlCenter",objNull];
		if (!isnull _control_center) then {
			[_control_center,localize "STR_MVT_CC_ACTION",{createDialog "MVT_ui_control";}] call FUNC(addAction);
		};
		_safe_zone = "ProtectionZone_Invisible_F" createVehicleLocal GVAR(MVT_POS);
		_safe_zone setPos GVAR(MVT_POS);
		_safe_zone setDir -211;
	
		_safe_zone = "ProtectionZone_F" createVehicleLocal GVAR(MVT_POS);
		_safe_zone setDir -211;
		_safe_zone setPos [GVAR(MVT_POS) select 0, GVAR(MVT_POS) select 1, -28.45];
		_safe_zone setObjectTexture [0,"#(argb,8,8,3)color(0,0,0,0.7,ca)"];
	};
} else {
	ERROR("MVT_Training_Ground marker not found!"),
};

TRACE("End");
TMFPROFILERSTOP;